package com.xbongbong.pro.help;

import com.xbongbong.paas.enums.MobileIndexModeEnum;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.pojo.vo.MobileModeGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.sys.domain.entity.UserConfigEntity;
import com.xbongbong.sys.enums.UserConfigEnum;
import com.xbongbong.sys.model.UserConfigModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shen.yang
 * @version v1.0
 * @Description
 * @date 2022/6/16 19:31
 * @since v1.0
 */
@Component
public class UserConfigHelp {

    private static final Logger LOG = LoggerFactory.getLogger(UserConfigHelp.class);

    @Resource
    private UserConfigModel userConfigModel;
    @Resource
    private CommonHelp commonHelp;

    public MobileModeGetVO getMobileMode(BaseDTO baseDTO) throws XbbException {
        MobileModeGetVO mobileModeGetVO = new MobileModeGetVO();
        UserConfigEntity userConfigEntity = userConfigModel.getByUserId(baseDTO.getUserId(),baseDTO.getCorpid(), UserConfigEnum.MOBILE_INDEX_MODE.getAlias());
        if (null != userConfigEntity){
            try {
                MobileIndexModeEnum mobileModeTypeEnum = MobileIndexModeEnum.getByCode(Integer.parseInt(userConfigEntity.getConfigValue()));
                if (null != mobileModeTypeEnum){
                    mobileModeGetVO.setMode(mobileModeTypeEnum.getCode());
                }else {
                    mobileModeGetVO.setMode(MobileIndexModeEnum.FOCUS_MODE.getCode());
                }
            }catch (Exception e){
                LOG.error("获取配置失败");
            }
        }else {
            // 用户是否有经典模式，有的话是默认不开启专注模式
            if (commonHelp.hasClassicMode(baseDTO.getCorpid())){
                mobileModeGetVO.setMode(MobileIndexModeEnum.CLASSIC_MODE.getCode());
            }else {
                mobileModeGetVO.setMode(MobileIndexModeEnum.FOCUS_MODE.getCode());
            }
        }
        return mobileModeGetVO;
    }
}
